package com.pdg.mcplugin.common.baseclasses;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.bukkit.ChatColor;

import com.pdg.mcplugin.common.interfaces.DataProvider;

public class DataProviderBase<E extends PluginBase> extends PluginClientBase<E> implements DataProvider {

	private static final String CLASS_NAME_SQLITE = "org.sqlite.JDBC";
	private static final String ERROR_COULD_NOT_LOAD_SQLITE = ChatColor.RED + "[%s] Could not load SQLite!";
	private static final String ERROR_COULD_NOT_CONNECT_TO_DATABASE = ChatColor.RED + "[%s] Failed to open connection to database!";
	private static final String ONE_SINGLE_QUOTE = "'";
	private static final String TWO_SINGLE_QUOTES = "''";
	private static final String DB_CHECK_FOR_TABLE_FORMAT = "SELECT `name` FROM `sqlite_master` WHERE `type`='table' AND `name`='%s';";
	private static final String ERROR_CHECKING_FOR_TABLE = "Error encountered checking for table `%s`.";
	private static final String DB_DROP_TABLE_FORMAT = "DROP TABLE IF EXISTS `%s`;";
	private static final String ERROR_DROPPING_TABLE = "Error encountered dropping table `%s`.";
	public DataProviderBase(E thePlugin,String theConnectionString) {
		super(thePlugin);
		setConnectionString(theConnectionString);
	}

	private void setConnectionString(String theConnectionString) {
		connectionString = theConnectionString;
	}

	//connection
	private Connection connection;
	private String connectionString;
	public Connection getConnection(){
		//if no connection exists
		if(connection==null){
			//make sure the data folder exists
			getPlugin().ensureDataFolder();
			try{
				//look for sqlite
				Class.forName(CLASS_NAME_SQLITE);
			} catch(Exception e){
				//error occurred, so no sql lite!
				getLogger().info(ERROR_COULD_NOT_LOAD_SQLITE);
				e.printStackTrace();
				return null;
			}
			try{
				//create a connection
				connection = DriverManager.getConnection(String.format(getConnectionString(),getPlugin().getDataFolder().toString()));
			}catch(Exception e){
				//error occurred, so no connection
				getLogger().info(ERROR_COULD_NOT_CONNECT_TO_DATABASE);
				e.printStackTrace();
				connection = null;
				return null;
			}
		}
		//yay! a connection
		return connection;
	}
	private String getConnectionString() {
		return connectionString;
	}	
	public static String sqlSafeString(String theName){
		return theName.replaceAll(ONE_SINGLE_QUOTE,TWO_SINGLE_QUOTES);
	}
	public boolean doesTableExist(String theTableName){
		try{
			//create a statement to check for inventory table
			Statement statement = getConnection().createStatement();
			ResultSet result = statement.executeQuery(String.format(DB_CHECK_FOR_TABLE_FORMAT,theTableName));

			return result.next();
		}catch(Exception e){
			//an error has occurred
			getLogger().severe(getPlugin().formatMessage(String.format(ERROR_CHECKING_FOR_TABLE,theTableName)));
			e.printStackTrace();
			return false;
		}	
	}
	public boolean dropTable(String theTableName){
		if(doesTableExist(theTableName)){
			try{
				//create a statement to check for inventory table
				Statement statement = getConnection().createStatement();
				statement.execute(String.format(DB_DROP_TABLE_FORMAT,theTableName));
				return true;
			}catch(Exception e){
				//an error has occurred
				getLogger().severe(getPlugin().formatMessage(String.format(ERROR_DROPPING_TABLE,theTableName)));
				e.printStackTrace();
				return false;
			}	
		}else{
			return false;
		}
	}
}
